<template>
  <div class="login-container">
    <van-nav-bar title="登 录" class="page-nav-bar" />
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field v-model="loginForm.mobile" name="mobile" placeholder="请输入手机号" type="number"
        maxlength="11" :rules="loginFormRules.mobile">
        <template #left-icon>
          <i class="iconfont icon-shouji"></i>
        </template>

      </van-field>
      <van-field v-model="loginForm.code" name="code" placeholder="请输入验证码" type="number"
        maxlength="6" :rules="loginFormRules.code">
        <template #left-icon>
          <i class="iconfont icon-yanzhengma"></i>
        </template>
        <template #button>
          <van-button v-if="countDownShow"  size="small" type="primary" native-type="button" class="send-code" @click='sendCode'>发送验证码</van-button>
          <van-count-down v-else @finish="countDownShow = true" format="ss s" :time="time" />
        </template>
      </van-field>
      <div class="submit-btn">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI, sendCodeAPI } from '@/api'
export default {
  name: 'LoginIndex',
  data () {
    return {
      loginForm: {
        mobile: '',
        code: ''
      },
      loginFormRules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3578]\d{9}$/, message: '手机号格式不正确' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' }
        ]
      },
      countDownShow: true,
      time: 60 * 1000
    }
  },
  methods: {
    //   提交表单
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data: { data } } = await loginAPI(this.loginForm)
        // console.log(data)
        this.$store.commit('initUser', data)
        this.$toast.success('登录成功')
      } catch (error) {
        if (error.response?.status === 400) {
          return this.$toast.fail('手机号或者验证码错误')
        }
        this.$toast.fail('系统异常，稍后重试')
      }
    },
    // 发送验证码
    async sendCode () {
      try {
        this.$refs.loginFormRef.validate('mobile')
      } catch (error) {
        return
      }
      // 发送验证码
      this.$toast.loading({
        message: '发送中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await sendCodeAPI(this.loginForm.mobile)
        this.countDownShow = false
        this.$toast.success('验证码发送成功')
      } catch (error) {
        if (error.response?.status === 429) {
          return this.$toast.fail('验证码发送太频繁，请稍后重试')
        }
        this.$toast.fail('发送验证码失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .iconfont {
    font-size: 34px;
  }
  .send-code {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    border: none;
    border-radius: 20px;
  }
  .submit-btn {
    margin: 53px 33px;
    .van-button--info {
      border: none;
      background-color: #6db4fb;
    }
    .van-button--round {
      border-radius: 10px;
    }
  }
}
</style>
